#pragma once
// #define USE_RKNNPOOL
#include <QObject>
#ifdef USE_RKNNPOOL
#include "rknnPool.hpp"
#endif
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/imgproc/types_c.h>
#include "mainwindow.h"
#include <QtDebug>
#include <QMutex>
#include <QSemaphore>
#include <QTimer>  // 添加这一行

#include <linux/videodev2.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include "rknnPool.hpp"
#include "rkYolov8_seg.hpp"


class RKNN_YOLOVX : public QThread {
    Q_OBJECT
public:

    explicit RKNN_YOLOVX(MainWindow *mainWindow,int cameras,std::string &model_name,const char* label_path);
    ~RKNN_YOLOVX();
    void stop() {
        if (testPool!=nullptr)
        {
            delete testPool;
            testPool=nullptr;
        }
        requestInterruption(); // 设置中断标志
        quit();
        wait();
    }
signals:
    void sendDataToWorker(const QString &data); // 主线程发射的信号
private:
    // rkYolov8_seg *rkYolov8_seg_obj;
    // image_buffer_t dst_img;
    // rknn_app_context_t rknn_app_ctx;
    // image_buffer_t src_image;
    // object_detect_result_list od_results;
    // rknn_lite *ptr;
    QMutex mutex; // 用于保护共享资源的互斥锁
    int ret;
    cv::VideoCapture capture;
    MainWindow *m_mainWindow;
    bool had_detected=false;
    bool enable=true;
    int _obj_id=-1;
    bool multy_classify=false;
    int fd;
    char text[100];
    int i = 0;
    unsigned char* dst_buf = NULL;


public:
    rknnPool<rkYolov8_seg, cv::Mat, cv::Mat>* testPool;
    bool get_detected();
    void set_detected(bool result);
    int get_ObjectId();
    void set_ObjectId(int id);
    void statusCtl(bool status);
    void set_classify(bool result);
    bool get_classify();
public slots:
    void run(cv::Mat &imageData);
    void run_bypass();
    void handleDataFromMainThread(const QString &data) {
        // 在子线程中处理来自主线程的数据
        qDebug() << "Received data in worker thread:在子线程中处理来自主线程的数据" << data;
    }
public slots:
    void handleDataFromL4V2Thread(cv::Mat &imageData);
};

